/**
 * 
 */
package controller.xml;

import model.session.ExecutableSessionBlock;
import model.session.ExecutionStatus;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link ExecutableSessionBlock} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class ExecutableXMLHandler extends XMLHandler {


	private Long				previousTime	= null;
	private ExecutionStatus		execstatus		= null;
	private String				description		= null;
	private ExecutableXMLStatus	status			= ExecutableXMLStatus.NONE;

	public ExecutableXMLHandler() {
	}


	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
		case SESSION_BLOCK:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		if (status == ExecutableXMLStatus.NONE)
			return false;

		if (status == ExecutableXMLStatus.SESSION_BLOCK && qName.equals(XMLBaliseFormat.SESSION_BLOCK))
		{// balise session block
			status = ExecutableXMLStatus.NONE;
			previousTime = ((SessionBlockXMLHandler) delegate).getPreviousTime();
			if (previousTime == null)
				throw new SAXException("problem with PREVIOUS TIME");

			delegate = null;
			return true;
		}
		if (status == ExecutableXMLStatus.DESCRIPTION && qName.equals(XMLBaliseFormat.EXECUTION_DESCRIPTION))
		{// balise descritpion
			setDescription();
			status = ExecutableXMLStatus.NONE;
			return true;
		}
		if (status == ExecutableXMLStatus.STATUS && qName.equals(XMLBaliseFormat.EXECUTION_STATUS))
		{// balise status
			setStatus();
			status = ExecutableXMLStatus.NONE;
			return true;
		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != ExecutableXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.SESSION_BLOCK))
		{// balise session block
			// set les donnees pour recupere le session block
			status = ExecutableXMLStatus.SESSION_BLOCK;
			delegate = new SessionBlockXMLHandler();
			return true;
		}

		if (arg2.equals(XMLBaliseFormat.EXECUTION_DESCRIPTION))
		{// balise descritpion
			status = ExecutableXMLStatus.DESCRIPTION;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.EXECUTION_STATUS))
		{// balise status
			status = ExecutableXMLStatus.STATUS;
			return true;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the execstatus
	 * 
	 * @return the execstatus
	 */
	public ExecutionStatus getExecstatus() {
		return execstatus;
	}

	/**
	 * Return the description.
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Return the previousTime.
	 * 
	 * @return the previousTime
	 */
	public Long getPreviousTime() {
		return previousTime;
	}

	/**
	 * Return the status.
	 * 
	 * @return the status
	 */
	public ExecutableXMLStatus getStatus() {
		return status;
	}

	/**
	 * Set the description .
	 */
	private void setDescription() {
		description = buffer.toString();
	}

	/**
	 * Set the {@link ExecutionStatus}.
	 * 
	 * @throws SAXException
	 */
	private void setStatus() throws SAXException {
		execstatus = ExecutionStatus.getValueFromXML(buffer.toString());
		if (execstatus == null)
			throw new SAXException("status unknown");

	}
}
